<template>
  <grid title="分类管理">
    <el-form inline
             style="width: 100%; text-align: left"
             label-width="60px"
             @submit.native.prevent="onSubmit"
             @reset.native.prevent="onReset"
             size="small">
      <el-form-item label="分类名">
        <el-input v-model="query.q"
                  placeholder="请输入关键词"
                  clearable></el-input>
      </el-form-item>

      <el-form-item label="发布状态"
                    label-width="80px">
        <el-select v-model="query.status">
          <el-option label="全部"
                     value="all"></el-option>
          <el-option label="未发布"
                     value="down"></el-option>
          <el-option label="发布中"
                     value="up"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   native-type="submit">筛选</el-button>
        <el-button native-type="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <el-button type="primary"
                   @click="addCate">新建分类</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list"
              border
              :resizable="false"
              stripe>
      <el-table-column type="index"
                       label="编号"
                       width="60"
                       align="center"></el-table-column>
      <el-table-column prop="name"
                       label="标签名">
      </el-table-column>
      <el-table-column prop="nums"
                       label="文章数">
        <template slot-scope="scope">
          <router-link :to="{
            name: 'topic',
            query: {
              cate: scope.row.id
            }
          }">
            <font color="#409EFF">{{scope.row.nums}}</font>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column prop="status"
                       label="状态">
        <template slot-scope="scope">
          <normal-status :data="scope.row"></normal-status>
        </template>
      </el-table-column>
      <el-table-column prop="created_at"
                       label="创建时间">
        <template slot-scope="scope">
          {{scope.row.created_at | timeFilter }}
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="240">
        <template slot-scope="scope">
          <el-button type="text"
                     size="small"
                     @click="editRow(scope.row.id)">修改</el-button>
          <template v-if="scope.row.status === 1">
            <el-button type="text"
                       @click="offlineRow(scope.row.id)"
                       size="small">下架</el-button>
          </template>
          <template v-else>
            <el-button type="text"
                       @click="onlineRow(scope.row.id)"
                       size="small">上架</el-button>
          </template>
          <el-button type="text"
                     @click="deleteRow(scope.row.id)"
                     size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total, prev, pager, next"
                   background
                   @current-change="fetch"
                   :current-page.sync="query.page"
                   :page-size="query.limit"
                   hide-on-single-page
                   style="display: flex;justify-content: center;"
                   :total="total">
    </el-pagination>

    <el-dialog :title="form.id ? '修改分类' : '新增分类'"
               :visible.sync="dialogVisible"
               width="650px"
               :show-close="false"
               center>
      <el-form ref="dialogForm"
               :model="form"
               label-width="120px"
               :rules="rules">
        <el-form-item label="名称"
                      prop="name">
          <el-input v-model="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="form.status"
                     active-text="显示"
                     :active-value="1"
                     :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onDialogSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </grid>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import listMixin from '@/mixin/list'
import { fetchView, store, edit, destory, offline, online } from './api'

export default {
  title: '分类管理',
  data () {
    return {
      query: {
        limit: 12,
        page: 1,
        q: undefined,
        status: 'all'
      },
      dialogVisible: false,
      form: {},
      formDefault: {
        id: undefined,
        name: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空' }
        ]
      }
    }
  },
  mixins: [listMixin],
  async asyncData ({ store, route }) {
    const { query } = route;
    return store.dispatch('cate/fetchList', { page: 1, limit: 12 })
  },
  computed: {
    ...mapGetters('cate', ['list', 'total'])
  },
  methods: {
    ...mapActions('cate', ['fetchList']),
    fetch () {
      this.fetchList(this.query)
    },
    addCate () {
      this.form = { ...this.formDefault }
      this.dialogVisible = true
      this.$nextTick(() => this.$refs.dialogForm.clearValidate())
    },
    async offlineRow (id) {
      await this.$confirm('此操作将在前台隐藏该分类 确定执行？', '警告')
      await offline(id)
      this.$message.success('操作成功')
      this.fetchList(this.query)
    },
    async onlineRow (id) {
      await this.$confirm('此操作将在前台显示该分类 确定执行？', '警告')
      await online(id)
      this.$message.success('操作成功')
      this.fetchList(this.query)
    },
    async editRow (id) {
      await fetchView(id).then(({ data: { data } }) => {
        this.form = this.filterForm(data)
      })
      this.dialogVisible = true
      this.$nextTick(() => this.$refs.dialogForm.clearValidate())
    },
    async deleteRow (id) {
      await this.$confirm('确定删除？', '警告')
      await destory(id)
      this.$message.success('删除成功')
      this.fetchList(this.query)
    },
    async onDialogSubmit () {
      await this.$refs.dialogForm.validate()

      if (typeof this.form.id === 'number') {
        await edit(this.form.id, this.form)
      } else {
        await store(this.form)
      }
      this.$message.success('保存成功')
      this.fetchList(this.query)
      this.dialogVisible = false
    }
  }
}
</script>

